package com.hundred.home.module.screen;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.citrus.turbine.Context;
import com.alibaba.citrus.turbine.Navigator;
import com.alibaba.citrus.util.StringUtil;
import com.hundred.comment.dao.dataobject.AppCommentDO;
import com.hundred.comment.manager.CommentManager;
import com.hundred.item.dao.dataobject.AppItemDO;
import com.hundred.item.manager.AppItemManager;
import com.hundred.user.dao.dataobject.AppUserDO;
import com.hundred.user.manager.UserManager;

public class Index {

	private static final Logger logger = LoggerFactory.getLogger(Index.class);

	@Autowired
    private HttpServletRequest request;
	@Autowired
	private AppItemManager itemManager;
	@Autowired
	private CommentManager commentManager;
	@Autowired
	private UserManager userManager;
	
	public void execute(Context context, Navigator nav) {
		
		String tagStr = request.getParameter("tag");
		int tag = 1;
		if(!StringUtil.isBlank(tagStr)){
			tag=Integer.parseInt(tagStr);
		}
		List<AppItemDO> resultList = itemManager.getAppItemList(tag);
		
		List<AppCommentDO> commentList = commentManager.getAppCommentList(tag);
		
		if(resultList!=null && resultList.size()>0){
			int k=20;
			for(int i=0;i<resultList.size();i++){
				for(int j=0;j<4;j++){
					k+=1;
					AppItemDO itemDO = resultList.get(i);
					itemDO.getCommentUserIcons().put("icon0"+k, commentList.get(j).getCommentContent());
				}
			}
		}
		
		context.put("resultList", resultList);
		
		for(AppItemDO item:resultList){
			logger.error("------"+item.getItemName());
		}
		
		String openIdStr = request.getParameter("openid");
		String openkeyStr = request.getParameter("openkey");
		context.put("openIdStr", openIdStr);
		context.put("openkeyStr", openkeyStr);
		AppUserDO userDO = userManager.getAppUser4Open(openIdStr, openkeyStr);

		String jifenOntheWay = "0"; 

		if(userDO!=null){
			context.put("userDO", userDO);
			if(userDO.getLastLoginIp().split("_0")!=null){
				String[] temp = userDO.getLastLoginIp().split("_0");
				int points = 100*temp.length;
				jifenOntheWay = String.valueOf(points);
			}
			context.put("jifenOntheWay", jifenOntheWay);
		}
			
	}


	public AppItemManager getItemManager() {
		return itemManager;
	}


	public void setItemManager(AppItemManager itemManager) {
		this.itemManager = itemManager;
	}


	public CommentManager getCommentManager() {
		return commentManager;
	}


	public void setCommentManager(CommentManager commentManager) {
		this.commentManager = commentManager;
	}


	public UserManager getUserManager() {
		return userManager;
	}


	public void setUserManager(UserManager userManager) {
		this.userManager = userManager;
	}
	
	

}
